package dream.web;

import dream.pojo.User;
import dream.service.ArticleService;
import dream.util.SessionUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/goodCount")
public class AddGoodCountServlet extends HttpServlet {
    private ArticleService articleService = new ArticleService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String aid = req.getParameter("aid");
        User user = SessionUtils.getLoginUser(req);

        if(user == null){
            return;
        }

        int count = articleService.SelectGood(user.getId(), Integer.valueOf(aid));

        if(count == 0){
            articleService.InsertGood(user.getId(), Integer.valueOf(aid));
            articleService.AddGoodCount(Integer.valueOf(aid));
            resp.getWriter().println("0");
        }else{
            resp.getWriter().println("1");
        }
    }
}
